package com.yenlien.traceyun.control.service;

import com.yenlien.traceyun.control.dao.BankInfoDao;
import com.yenlien.traceyun.control.dao.BankLevelDao;
import com.yenlien.traceyun.control.entity.BankInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class BankInfoService {
    @Autowired
    private BankInfoDao bankInfoDao;

    @Autowired
    private BankLevelDao bankLevelDao;


    public Page<BankInfo> findByAll(final BankInfo bankInfo) {
        List<Map<String, Object>> all = bankInfoDao.findAllAndLevel(bankInfo.getPage(), bankInfo.getPageSize());
        List<BankInfo> list = new ArrayList<>();
        int total = 0;
        for (Map<String, Object> map : all) {
            BankInfo info = new BankInfo();
            info.setId(map.get("id").toString());
            info.setName(map.get("name").toString());
            info.setPid(map.get("pid").toString());
            info.setTel(map.get("tel").toString());
            info.setBankName(map.get("bankName").toString());
            total = Integer.valueOf(map.get("totalPage").toString());
            list.add(info);
        }
        Pageable pageable = PageRequest.of(bankInfo.getPage(), bankInfo.getPageSize(), Sort.Direction.DESC, "createTime");
        Page<BankInfo> pipelinePage = new PageImpl<>(list, pageable, total);
        return pipelinePage;
    }

    /**
     * 保存
     */
    public Object save(BankInfo bankInfo) {
        if (!StringUtils.isEmpty(bankInfo.getId())) {
            bankInfoDao.delete(bankInfo);
        }
        //创建企业账户
        bankInfo.setCreateTime(new Date());
        return bankInfoDao.save(bankInfo);
    }

    /**
     * 根据id查询
     */
    public Object findById(String id) {
        List<Map<String, Object>> li = bankInfoDao.findById2(id);
        BankInfo bankInfo = new BankInfo();
        bankInfo.setId(li.get(0).get("id").toString());
        bankInfo.setName(li.get(0).get("name").toString());
        bankInfo.setPid(li.get(0).get("pid").toString());
        bankInfo.setBankName(li.get(0).get("bankName").toString());
        bankInfo.setAlias(li.get(0).get("alias").toString());
        return bankInfo;
    }

    /**
     * 查询电话号码是否重复
     */
    public BankInfo findByTel(String tel) {
        return bankInfoDao.findBytel(tel);
    }

    /**
     * 查询所有列表总行
     */
    public Object findByBankLevel() {
        return bankLevelDao.findAll();
    }


    /**
     * 根据 companyId    查询银行bankID
     */
    public BankInfo findByBankId(String companyId) {
        return bankInfoDao.findByCompanyId(companyId);
    }

}
